//
//  InvestmentVC.m
//  TenThousand
//
//  Created by 我的 on 16/6/20.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "InvestmentVC.h"
#import "InvestmentTableViewCell.h"
#import "Toubiaojilu.h"

@interface InvestmentVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _currentPage;
    BOOL _isLastPage;
}
@property (nonatomic, strong) NSMutableArray *touziArr;

@end
#define headerTitle @[@"投资人",@"投资记录（元）",@"投资时间"]
@implementation InvestmentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"投资计划";
    [self refresh];
    [self.view addSubview:self.investTableView];
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:NO];
    } right:nil rightClick:nil];
    
    
}

-(NSMutableArray *)touziArr{
    if (!_touziArr) {
        _touziArr = [NSMutableArray array];
    }
    return _touziArr;
}

- (UITableView *)investTableView{
    
    if (!_investTableView) {
        _investTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-50) style:UITableViewStylePlain];
        _investTableView.delegate = self;
        _investTableView.dataSource = self;
    }
    return _investTableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.touziArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InvestmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"INVESTMENTCELL"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"InvestmentTableViewCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    if (self.touziArr.count) {
        Toubiaojilu *model = self.touziArr[indexPath.row];
        [cell setdatamodel:model];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
//添加header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 50)];
    header.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < headerTitle.count; i++) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(Screen_Width/3 * i, 0, Screen_Width/3, 50)];
        lable.text = headerTitle[i];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.font = [UIFont systemFontOfSize:15.0f];
        [header addSubview:lable];
        
    }
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, 49, Screen_Width, 1)];
    line.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1.00];
    [header addSubview:line];
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

-(void)getBorrowTenberListWithPage:(NSInteger)page{
    
    NSString *apage = [NSString stringWithFormat:@"%ld",page];
    
    NSDictionary *para = @{
                           @"Borrowid":self.Borrowid,
                           @"PageIndex":apage
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Borrow.GetBorrowTenberSuccess" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        
        if (page == 1) {
            NSArray *array = [object[@"Data"] firstObject][@"ItemList"];
            
            [self.touziArr removeAllObjects];
            _currentPage = 1;
            _isLastPage = NO;
            for (NSDictionary *dic in array) {
                Toubiaojilu *toubiao = [Toubiaojilu mj_objectWithKeyValues:dic];
                [self.touziArr addObject:toubiao];
            }
            
        }else
        {
           NSArray *array = [object[@"Data"] firstObject][@"ItemList"];
            if (array && [array count]) {
                _currentPage++;
                _isLastPage = NO;
                for (NSDictionary *dic in array) {
                    Toubiaojilu *toubiao = [Toubiaojilu mj_objectWithKeyValues:dic];
                    [self.touziArr addObject:toubiao];
                }
                
            }else
            {
                _isLastPage = YES;
            }
            
        }
        
        [_investTableView reloadData];
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
        
    } progress:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self.investTableView.mj_header beginRefreshing];

    
}
//MJ刷新
-(void)refresh
{
    
    //下拉刷新
    self.investTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                  {
                                      
                                      [self getBorrowTenberListWithPage:1];
                                      
                                      [self.investTableView.mj_header endRefreshing];
                                  }];
        //上拉加载
        self.investTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
    
    
    
            if (_isLastPage) {
                [self.investTableView.mj_footer endRefreshing];
                return;
            }
    
            [self getBorrowTenberListWithPage:_currentPage++];
    
            [self.investTableView.mj_footer endRefreshing];
        }];
        
    
}



@end
